package com.orange.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.orange.commons.Constant;
import com.orange.utils.WebMvcUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Emp
 * @projectName: crm
 * @package: com.powernode.controller
 * @className: CaptchaController
 * @description: TODO
 * @date: 2021/11/29 13:20
 * @version: 1.0
 */
@Controller
public class CaptchaController {

    /**
     * HuTool生成验证码
     * @param response
     */
    @RequestMapping("captcha.do")
    public void captcha(HttpServletResponse response){
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 40, 1, 100);
        String code = lineCaptcha.getCode();
        //将验证码在session中保存一份
        WebMvcUtil.getSession().setAttribute(Constant.CAPTCHA_CODE,code);
        try {
            lineCaptcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
